/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.HinhAnhCuaMatHangDAO;
import com.miracleteam.miracle.entities.Hinhanhcuamathang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class HinhAnhCuaMatHangDAOImpl implements HinhAnhCuaMatHangDAO {
     Logger logger= Logger.getLogger(HinhAnhCuaMatHangDAOImpl.class);
     public List LayDSHinhAnh(String maMH)
     {
         List ds = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "from Hinhanhcuamathang where daXoa=0 and mathangcuagianhang.maMatHangCuaGianHang =:maMH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maMH", maMH);
            ds = query.list();

        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        return ds;
     }
       public String PhatSinhMaHinhAnh()
    {
        String kq="HAMH-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
       public boolean ThemHinhAnh(Hinhanhcuamathang ha)
       {
            Session session = HibernateUtil.getSessionFactory().openSession();
         if(ha==null)
             return false;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(ha);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
       }
       public int  XoaAnh(String ma)
       {
            int kq=0;
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "update Hinhanhcuamathang ha set ha.daXoa=1 where  ha.mathangcuagianhang.maMatHangCuaGianHang =:maMH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maMH", ma);
            kq = query.executeUpdate();

            } catch (Exception e) {
                logger.error(e.getMessage());
                System.out.println(e.getMessage());
            }
            finally{
                session.close();
            }
            return kq;
       }
}
